<?php
class Products extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Organizations the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'products';
	}
	
	public function rules()
	{
		return array(
			array('product_name, product_type_ID', 'required'),
			array('product_name', 'length', 'max'=>50),
			array('product_name', 'unique', 'criteria' => array('select'=>$this->tableName(),
															'condition' => 'product_name=:product_name',
															'params'=>array(':product_name'=>$this->product_name)), 
															),
		);
	}
	
	public function relations()
	{
		return array(
			'tanks'=>array(self::HAS_MANY, 'Tanks', 'tank_ID'),
			'auctions'=>array(self::HAS_MANY, 'Auctions', 'auction_ID'),
			'orgProRelation'=>array(self::HAS_MANY, 'OrgProductRelation', 'product_ID'),
			'productType'=>array(self::BELONGS_TO, 'ProductTypes', 'product_type_ID'),
		);
	}
	
	public function attributeLabels()
	{
		return array(
			'product_name'=>'Product Name',
			'product_type'=>'Product Type',
		);
	}

	public function getProductsInfo()
	{
		$connection = Yii::app()->db;
		$sql = "SELECT * FROM ".$this->tableName();
		$command = $connection->createCommand($sql);
		$dataReader = $command->query();
		$res=array();
		foreach($dataReader as $row)
		{
			$key=$row['product_ID'];
			$res[$key]=$row['product_name'];
		}
		return $res;
	}
	
	public function getProductName($productID) {
		$sql = "SELECT product_name FROM ".$this->tableName()." WHERE product_ID='".$productID."'";
		$resultset = $this->findAllBySql($sql);
		
		foreach ($resultset as $row) {
			$productName = $row->product_name;
		}
		
		return $productName;
	}
}
?>
